define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        income: function () {
            $(document).on("click", ".project_name", function (e) {
                var xh = (e.target.dataset.xh) * 1
                var url = 'basic/project/select';
                var msg = '账户列表';
                var width = '1200px';
                var height = '800px';
                var area = [$(window).width() > 800 ? (width ? width : '800px') : '95%', $(window).height() > 600 ? (height ? height : '600px') : '95%'];
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: area,
                    callback: function (value) {
                        //这个value就是传上来的值
                        $('.project_id' + xh).val(value.id)
                        $('.project_name' + xh).val(value.name)
                        var num = xh += 1;
                        var html = getHtmlProject(num);
                        if ($('.project' + num).length <= 0) {
                            $('#project').append(html)
                        }
                    }
                };
                Fast.api.open(url, msg, options);
            })

            $(document).on("click", ".goods_del", function (e) {
                var xh = (e.target.dataset.xh) * 1
                if (xh != 1) {
                    $('.project' + xh).remove();
                }
            })


            $(document).on("keyup", ".goods_qty", function (e) {
                var xh = $(this).attr('data-xh')
                if ($('.goods_id' + xh).val()) {
                    var qty = $(this).val();
                    var price = $('.goods_price' + xh).val();
                    qty = parseFloat(qty)
                    price = parseFloat(price)
                    $('.goods_amount' + xh).val((qty * price).toFixed(2));
                } else {
                    $(this).val('');
                }
            });

            $(document).on("keyup", ".goods_price", function (e) {
                var xh = $(this).attr('data-xh')
                if ($('.goods_id' + xh).val()) {
                    var price = $(this).val();
                    var qty = $('.goods_qty' + xh).val();
                    qty = parseFloat(qty)
                    price = parseFloat(price)
                    $('.goods_amount' + xh).val((qty * price).toFixed(2));
                    getSumAssets()
                } else {
                    $(this).val('');
                }
            });
            $(document).on("keyup", ".project_price", function (e) {
                var xh = $(this).attr('data-xh')
                if ($('.project_id' + xh).val()) {
                    getSumAssets()
                } else {
                    $(this).val('');
                }
            });

            $(document).on("keyup", ".goods_amount", function (e) {
                var xh = $(this).attr('data-xh')
                var amount = $(this).val();
                if ($('.goods_id' + xh).val() && amount) {

                    var qty = $('.goods_qty' + xh).val();
                    qty = parseFloat(qty)
                    amount = parseFloat(amount)
                    $('.goods_price' + xh).val((amount / qty).toFixed(2));
                } else {
                    $(this).val('');
                }
            });

            Form.api.bindevent($("form[role=form]"), function () {
            }, function () {
            }, function (success, error) {
                layer.confirm('请仔细核对单据！避免出错。', {
                    btn: ['确定', '取消'] //按钮
                }, function (index) {
                    Form.api.submit($("form[role=form]"), function (data, ret) {
                        if(ret.code == 1){
                            setTimeout(function(){
                                location.reload()
                            },1000)
                        }
                    });
                    Layer.close(index);
                }, function () {
                });
                return false;

            });
        },
        expenditure: function () {
            $(document).on("click", ".project_name", function (e) {
                var xh = (e.target.dataset.xh) * 1
                var url = 'basic/project/select';
                var msg = '账户列表';
                var width = '1200px';
                var height = '800px';
                var area = [$(window).width() > 800 ? (width ? width : '800px') : '95%', $(window).height() > 600 ? (height ? height : '600px') : '95%'];
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: area,
                    callback: function (value) {
                        //这个value就是传上来的值
                        $('.project_id' + xh).val(value.id)
                        $('.project_name' + xh).val(value.name)
                        var num = xh += 1;
                        var html = getHtmlProject(num);
                        if ($('.project' + num).length <= 0) {
                            $('#project').append(html)
                        }
                    }
                };
                Fast.api.open(url, msg, options);
            })

            $(document).on("click", ".goods_del", function (e) {
                var xh = (e.target.dataset.xh) * 1
                if (xh != 1) {
                    $('.project' + xh).remove();
                }
            })


            $(document).on("keyup", ".goods_qty", function (e) {
                var xh = $(this).attr('data-xh')
                if ($('.goods_id' + xh).val()) {
                    var qty = $(this).val();
                    var price = $('.goods_price' + xh).val();
                    qty = parseFloat(qty)
                    price = parseFloat(price)
                    $('.goods_amount' + xh).val((qty * price).toFixed(2));
                } else {
                    $(this).val('');
                }
            });

            $(document).on("keyup", ".goods_price", function (e) {
                var xh = $(this).attr('data-xh')
                if ($('.goods_id' + xh).val()) {
                    var price = $(this).val();
                    var qty = $('.goods_qty' + xh).val();
                    qty = parseFloat(qty)
                    price = parseFloat(price)
                    $('.goods_amount' + xh).val((qty * price).toFixed(2));
                    getSumAssets()
                } else {
                    $(this).val('');
                }
            });
            $(document).on("keyup", ".project_price", function (e) {
                var xh = $(this).attr('data-xh')
                if ($('.project_id' + xh).val()) {
                    getSumAssets()
                } else {
                    $(this).val('');
                }
            });

            $(document).on("keyup", ".goods_amount", function (e) {
                var xh = $(this).attr('data-xh')
                var amount = $(this).val();
                if ($('.goods_id' + xh).val() && amount) {

                    var qty = $('.goods_qty' + xh).val();
                    qty = parseFloat(qty)
                    amount = parseFloat(amount)
                    $('.goods_price' + xh).val((amount / qty).toFixed(2));
                } else {
                    $(this).val('');
                }
            });

            Form.api.bindevent($("form[role=form]"), function () {
            }, function () {
            }, function (success, error) {
                layer.confirm('请仔细核对单据！避免出错。', {
                    btn: ['确定', '取消'] //按钮
                }, function (index) {
                    Form.api.submit($("form[role=form]"), function (data, ret) {
                        if(ret.code == 1){
                            setTimeout(function(){
                                location.reload()
                            },1000)
                        }
                    });
                    Layer.close(index);
                }, function () {
                });
                return false;

            });
        },
    };

    function getSumAssets() {
        var inputs = $('.project_price');
        var sum_amount = 0;
        inputs.each(function () {
            sum_amount = sum_amount + $(this).val() * 1;
        });
        $('input[name=amount]').val(sum_amount);
    }


    function getHtmlProject(num) {
        var html = '        <div class="form-group col-xs-12 project' + num + '" style="margin-left: 5%">\n' +
            '            <label class="control-label col-xs-1 col-sm-1">' + num + '</label>\n' +
            '            <div class="col-xs-3 col-sm-3 cy-div">\n' +
            '  <input data-rule=""\n' +
            '                           class="form-control project_id project_id' + num + '" readonly data-xh="1"\n' +
            '                           name="project_id[]" type="hidden" value="" data-field="number" placeholder="账户id">\n' +
            '                <input data-rule=""\n' +
            '                       class="form-control project_name project_name' + num + '" readonly name="project_name[]" data-xh="' + num + '" type="text"\n' +
            '                         value="" placeholder="点击选择项目">\n' +
            '            </div>\n' +
            '            <div class="col-xs-1 col-sm-1 cy-div">\n' +
            '                <input data-rule=""\n' +
            '                       class="form-control project_price project_price' + num + '" name="project_price[]" type="text" value="" data-xh="' + num + '"\n' +
            '                       placeholder="金额">\n' +
            '            </div>\n' +
            '            <div class="col-xs-2 col-sm-2 cy-div">\n' +
            '                <input data-rule=""\n' +
            '                       class="form-control project_remarks project_remarks' + num + '" name="project_remarks[]" type="text" value="" data-xh="' + num + '"\n' +
            '                       placeholder="备注">\n' +
            '            </div>\n' +
            '   <div data-xh="' + num + '" class="col-xs-1 col-sm-1 cy-div goods_del" style="line-height: 35px;padding-left: 10px; cursor: pointer;">\n' +
            '                    删除\n' +
            '                </div>' +
            '        </div>'

        return html
    }
    return Controller;
});
